package com.pctx.bibacrmrbac.controller;

import com.github.pagehelper.PageHelper;
import com.pctx.bibacommon.result.Result;
import com.pctx.bibacrmrbac.pageHelper.PageInfo;
import com.pctx.bibacrmrbac.pojo.Permission;
import com.pctx.bibacrmrbac.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by chason on 2018/2/24.11:25
 */
@RestController
@RequestMapping("permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @GetMapping("list4company")
    public Result getByCompanyUuid(@RequestParam(value = "companyUuid", required = true) String companyUuid) throws Exception {
        List<Permission> list = permissionService.getByCompanyUuid(companyUuid);
        if (CollectionUtils.isEmpty(list))
            return Result.error("");
        else
            return Result.success(list);
    }

    @GetMapping("list4companyPage")
    public Result getByCompanyUuidPage(@RequestParam(value = "companyUuid", required = true) String companyUuid,
                                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
        PageHelper.startPage(page, pageSize);
        List<Permission> list = permissionService.getByCompanyUuid(companyUuid);

        return Result.success(new PageInfo<Permission>(list));
    }

    @GetMapping("list4companynotowned")
    public Result getByCompanyUuid4NotOwned(@RequestParam(value = "companyUuid", required = true) String companyUuid) throws Exception {
        List<Permission> list = permissionService.getByCompanyUuid4NotOwned(companyUuid);

        return Result.success(list);
    }

    @PostMapping("companyadd")
    public Result addCompanyPermission(@RequestParam(value = "companyUuid", required = true) String companyUuid,
                                       @RequestParam(value = "permissionId", required = true) Integer permissionId) throws Exception {
        boolean result = permissionService.addCompanyPermission(companyUuid, permissionId);
        if (result)
            return Result.success(null, "添加成功");
        else
            return Result.error("添加失败");
    }

    @GetMapping("del")
    public Result del(@RequestParam(value = "uuid", required = true) String uuid) throws Exception {
        boolean result = permissionService.delByUuid(uuid);
        if (result)
            return Result.success(null, "删除成功");
        else
            return Result.error("删除失败");
    }
}
